<template>
  <Dialog title="批量填充" v-model="dialogVisible">
    <el-form ref="formRef" :model="formData" label-width="120px" v-loading="formLoading">
      <el-form-item label="选择仓库" prop="stockId">
        <el-input v-model="stockId" placeholder="请选择仓库" @click="selectStock" >
          <template #append>
            <Icon icon="ep:search" class="mr-5px" /> 
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="批号" prop="no">
        <el-input v-model="formData.no" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="基本数量" prop="quantity">
        <el-input v-model="formData.quantity" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="基本含税单价" prop="basicTaxPrice">
        <el-input v-model="formData.taxPrice" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="基本不含税单价" prop="basicNoTaxPrice">
        <el-input v-model="formData.basicNoTaxPrice" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="含税单价" prop="taxPrice">
        <el-input v-model="formData.taxPrice" placeholder="请输入" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
  <Stock
    ref="selectStockRef"
    @success="handleStockChange"
  />
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { ProductCategoryApi, ProductCategoryVO } from '@/api/erp/product/category'
import { defaultProps, handleTree } from '@/utils/tree'
import { CommonStatusEnum } from '@/utils/constants'
import Stock from './Stock.vue'
/** ERP 产品分类 表单 */
defineOptions({ name: 'ProductCategoryForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  id: undefined,
  stockId: undefined,
  no: undefined,
  quantity: undefined,
  basicNoTaxPrice: undefined,
  basictaxPrice: undefined,
  taxPrice: undefined
})
const selectStockRef = ref()
const selectStock = ()=>{
  selectStockRef.value.open();
}

const formRef = ref() // 表单 Ref
const productCategoryTree = ref() // 树形结构

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ProductCategoryApi.getProductCategory(id)
    } finally {
      formLoading.value = false
    }
  }
  await getProductCategoryTree()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ProductCategoryVO
    await ProductCategoryApi.createProductCategory(data)
    message.success(t('common.createSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    stockId: undefined,
    no: undefined,
    quantity: undefined,
    basicNoTaxPrice: undefined,
    basictaxPrice: undefined,
    taxPrice: undefined
  }
  formRef.value?.resetFields()
}

/** 获得产品分类树 */
const getProductCategoryTree = async () => {
  productCategoryTree.value = []
  const data = await ProductCategoryApi.getProductCategoryList()
  const root: Tree = { id: 0, name: '顶级产品分类', children: [] }
  root.children = handleTree(data, 'id', 'parentId')
  productCategoryTree.value.push(root)
}
</script>
